package com.ruoyi.file.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.config.AliOSSConfig;
import com.ruoyi.file.domain.FileDO;
import com.ruoyi.file.domain.FileNoticeImagesDO;
import com.ruoyi.file.domain.vo.file.FileCreateReqVO;
import com.ruoyi.file.domain.vo.file.FilePresignedUrlRespVO;
import com.ruoyi.file.domain.vo.file.FileRespVO;
import com.ruoyi.file.domain.vo.file.FileUploadReqVO;
import com.ruoyi.file.mapper.FileMapper;
import com.ruoyi.file.service.FileService;
import com.ruoyi.result.utils.AliOSSUtils;
import lombok.Data;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 文件 Service 实现类
 *
 * @author 芋道源码
 */
@Service
public class FileServiceImpl  extends ServiceImpl<FileMapper, FileDO> implements FileService {


    @Resource
    private FileMapper fileMapper;
    @Autowired
    private AliOSSUtils aliOSSUtils;

    @Autowired
    private AliOSSConfig aliOSSConfig;

    @Override
    @SneakyThrows
    public FileRespVO createFile(FileUploadReqVO uploadReqVO) {
        MultipartFile file = uploadReqVO.getFile();
        String name = file.getOriginalFilename();
        String path = uploadReqVO.getPath();
        String type = file.getContentType();
        // 调用阿里云oss工具类进行上传
        String url = aliOSSUtils.upload(file);

        // 保存到数据库
        FileDO fileDO = new FileDO();
        fileDO.setName(name);
        fileDO.setPath(path);
        fileDO.setUrl(url);
        fileDO.setType(type);
        fileDO.setDeleted(false);
        fileDO.setCreateTime(LocalDateTime.now());
        fileDO.setUpdateTime(LocalDateTime.now());
        fileMapper.insertFile(fileDO);
        FileRespVO fileRespVO = new FileRespVO();
        BeanUtils.copyProperties(fileDO, fileRespVO);
        return fileRespVO;
    }

    @Override
    public List<FileRespVO> getNoticeImages(Long noticeId) {
        // 查询数据库中的图片记录
        List<FileDO> fileList = fileMapper.selectNoticeImages(noticeId);
        if(fileList == null){
            return null;
        }
        return fileList.stream()
                .map(fileDO -> {
                    FileRespVO fileRespVO = new FileRespVO();
                    BeanUtils.copyProperties(fileDO, fileRespVO);
                    return fileRespVO;
                })
                .collect(Collectors.toList());
    }

    @Override
    public void deleteFile(Long id) throws Exception {


        // 删除记录
        fileMapper.deleteById(id);
    }



    @Override
    public void submitImageIds(Long noticeId, List<Long> imageIds) {
        // 删除 noticeId 对应的所有 fileIds
        fileMapper.deleteByNoticeId(noticeId);

        // 插入新的 fileIds
        if (!imageIds.isEmpty()) {
            List<FileNoticeImagesDO> newFileNoticeImages = imageIds.stream()
                    .map(fileId -> {
                        FileNoticeImagesDO fileNoticeImagesDO = new FileNoticeImagesDO();
                        fileNoticeImagesDO.setNoticeId(noticeId);
                        fileNoticeImagesDO.setFileId(fileId);
                        return fileNoticeImagesDO;
                    })
                    .collect(Collectors.toList());
            fileMapper.insertBatch(newFileNoticeImages);
        }
    }
}
